package net.lainiao.reptile;

import org.apache.commons.codec.digest.DigestUtils;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;
import redis.clients.jedis.JedisPoolConfig;

/**
 * Created by Administrator on 2017/1/11.
 */

@Service
public class RedisHelper {
    @Value("${redis.host:127.0.0.1}")
    private String host;
    @Value("${redis.port:6379}")
    private int port;
    @Value("${redis.password:}")
    private String password;

    public String getHost() {
        return host;
    }

    public void setHost(String host) {
        this.host = host;
    }

    public int getPort() {
        return port;
    }

    public void setPort(int port) {
        this.port = port;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    private JedisPool wpool = null;
    private Logger logger = LoggerFactory.getLogger(getClass());
    private final static String REDIS_SET_NAME="REPTILE_PROCEED";
    private final static String REDIS_SET_Add1="REPTILE_PROCEED_RECORE1";
    private final static String REDIS_SET_Add2="REPTILE_PROCEED_RECORE2";

    public void refresh(){
        if (wpool!=null){
            this.wpool.destroy();
            this.wpool = null;
        }
    }

    public synchronized JedisPool getWritePool() {
        if (wpool == null) {
            JedisPoolConfig config = new JedisPoolConfig();
            config.setMaxTotal(50);
            config.setMaxIdle(20);
            config.setMinIdle(10);
            config.setMaxWaitMillis(1000 * 20);
            logger.info("create redisPool host={},port={}",this.host,this.port);
            if (StringUtils.isNotBlank(password)){
                wpool = new JedisPool(config, this.host, this.port, 0, password);
            }else {
                wpool = new JedisPool(config, this.host, this.port, 0);
            }
        }
        return wpool;
    }

    private Jedis getWClient() {
        return getWritePool().getResource();
    }

    public boolean isProcessedForCommon(String url,String setName){
        Jedis jedis = null;
        boolean res = false;
        try {
            jedis = getWClient();
            String sign = DigestUtils.md5Hex(url);
            res = jedis.sismember(setName,sign);
        }catch (Exception e){
            logger.warn(e.getMessage());
            refresh();
        }finally {
            if (jedis!=null){
                jedis.close();
            }
        }
        return res;
    }

    public void addProcessForCommon(String url,String setName){
        Jedis jedis = null;
        try {
            jedis = getWClient();
            String sign = DigestUtils.md5Hex(url);
            jedis.sadd(setName,sign);
        }catch (Exception e){
            logger.warn(e.getMessage());
            refresh();
        }finally {
            if (jedis!=null){
                jedis.close();
            }
        }
    }

    public boolean isProcessed(String url) {
        return isProcessedForCommon(url,REDIS_SET_NAME);
    }

    public void addProcessed(String url) {
        addProcessForCommon(url,REDIS_SET_NAME);
    }
    public boolean isProcessed1(String url) {
        return isProcessedForCommon(url,REDIS_SET_Add1);
    }

    public void addProcessed1(String url) {
        addProcessForCommon(url,REDIS_SET_Add1);
    }
    public boolean isProcessed2(String url) {
        return isProcessedForCommon(url,REDIS_SET_Add2);
    }

    public void addProcessed2(String url) {
        addProcessForCommon(url,REDIS_SET_Add2);
    }
}
